
function sliceStr(str,num){
    if(typeof str != 'string') return '';
    const N = str.length-num>0? num:str.length-1;
    const result = str.slice(0,N);
    return result;
};

// 高亮替换
function selectStr(Oldstr,SelectKey){
    const reg = new RegExp("(" + SelectKey + ")", "g");
    return Oldstr.replace(reg,`<s style='color:red'>${SelectKey}</s>`);
};

//正则验证 
function validation(key,value){
    if(value.trim() === '') return true;
    let message;
    let reg;

    switch(key){
        case 'idCard':
            reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;  
        break
        case "email":
            reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
            break
        case "phone":
            reg = /^1(3|4|5|7|8)\d{9}$/;
            break
        default:
    }
    message = reg.test(value)?true:false;
    return message
}
//解析网站url
function GetUrlParams(URL){
    let url = URL?URL:document.location.toString();
    let arrObj = url.split("?");
    let params = Object.create(null);
    if(arrObj.length > 1){
        arrObj = arrObj[1].split("&");
        arrObj.forEach(item => {
            item = item.split("=");
            params[item[0]] = item[1]
        })
    }
    return params;
}   
//通过数据集id往上找分类和大类
function findFather(id,name,arr,vue){
    let dataNavs = [];
    if(arr){
        dataNavs = arr
    }else{
        dataNavs = JSON.parse(localStorage.getItem('DATA_NAVS'))
    }
    for(var i = 0; i < dataNavs.length;i++){
        if(dataNavs[i][name] && dataNavs[i][name] == id){
            dataNavs[i];
            if(name == 'dataSetId'){
                vue.dataNavs.push({
                    name:dataNavs[i].dataSetName,
                    directoryId:dataNavs[i].directoryId,
                    parentId:'',
                    type:dataNavs[i].type?dataNavs[i].type:null,
                    url:null
                })
            }else{
                if(dataNavs[i].type != 5){
                    vue.dataNavs.unshift({
                        name:dataNavs[i].name,
                        directoryId:dataNavs[i].directoryId,
                        parentId:dataNavs[i].parentId,
                        type:dataNavs[i].type?dataNavs[i].type:null,
                        url:`/pages/dataView.html?type=${dataNavs[i].type}&id=${dataNavs[i].parentId?dataNavs[i].parentId:dataNavs[i].directoryId}`
                    })
                }else{
                    vue.dataNavs.unshift({
                        name:dataNavs[i].name,
                        directoryId:dataNavs[i].directoryId,
                        parentId:dataNavs[i].parentId,
                        type:5,
                        url:`/pages/nNodes.html`
                    })
                }
                
            }   
        }else if(dataNavs[i].children){
            findFather(id,name,dataNavs[i].children,vue);
        }else if(dataNavs[i].list){
            findFather(id,name,dataNavs[i].list,vue);
        }
    }
}
export {
    sliceStr,
    selectStr,
    validation,
    GetUrlParams,
    findFather
}